/*
 * Copyright 2016 lizhaotailang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.marktony.zhihudaily.database

import android.arch.persistence.room.Database
import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.content.Context
import com.marktony.zhihudaily.data.*
import com.marktony.zhihudaily.database.dao.*

/**
 * Created by lizhaotailang on 2017/6/15.
 *
 * Main database description.
 */

@Database(entities = [
    ZhihuDailyNewsQuestion::class,
    DoubanMomentNewsPosts::class,
    GuokrHandpickNewsResult::class,
    ZhihuDailyContent::class,
    DoubanMomentContent::class,
    GuokrHandpickContentResult::class
], version = 1)
abstract class AppDatabase : RoomDatabase() {

    abstract fun zhihuDailyNewsDao(): ZhihuDailyNewsDao

    abstract fun doubanMomentNewsDao(): DoubanMomentNewsDao

    abstract fun guokrHandpickNewsDao(): GuokrHandpickNewsDao

    abstract fun zhihuDailyContentDao(): ZhihuDailyContentDao

    abstract fun doubanMomentContentDao(): DoubanMomentContentDao

    abstract fun guokrHandpickContentDao(): GuokrHandpickContentDao

    companion object {
        const val DATABASE_NAME = "paper-plane-db"

        private var INSTANCE: AppDatabase? = null

        private val lock = Any()

        fun getInstance(context: Context): AppDatabase {
            synchronized(lock) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(
                            context.applicationContext,
                            AppDatabase::class.java, DATABASE_NAME
                    ).build()
                }
                return INSTANCE as AppDatabase
            }
        }
    }

}
